﻿using Basic.FlowModel.AuditCopyLog;
using Basic.FlowModel.DB;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FlowDAL.lmpl
{
    internal class FlowAuditCopyLogDAL : BasicDAL<DBFlowAuditCopyLog, long>, IFlowAuditCopyLogDAL
    {
        public FlowAuditCopyLogDAL ( IRepository<DBFlowAuditCopyLog> basicDAL ) : base(basicDAL, "audit.copy.msg")
        {
        }
        public AuditCopyDto[] Adds ( long logId, AuditCopyLogAdd[] adds )
        {
            DateTime now = DateTime.Now;
            DBFlowAuditCopyLog[] logs = adds.ConvertAll(a => new DBFlowAuditCopyLog
            {
                AddTime = now,
                FromEmpId = a.FromEmpId,
                ToEmpId = a.ToEmpId,
                MsgType = a.MsgType,
                ItemId = a.ItemId,
                LogId = logId,
                NodeId = a.NodeId,
                Status = FlowRemoteModel.FlowMsgStatus.待发送,
                Id = IdentityHelper.CreateId()
            });
            this._BasicDAL.Insert(logs);
            return logs.ConvertMap<DBFlowAuditCopyLog, AuditCopyDto>();
        }
        public void SetStatus ( AuditCopyStatus[] sets )
        {
            if ( !this._BasicDAL.Update(sets.ConvertAll(a => new DBFlowAuditCopyLog
            {
                Id = a.Id,
                Status = a.Status,
                RetryNum = a.RetryNum,
                Error = a.Error,
                SendTime = a.SendTime
            }), "Status", "RetryNum", "Error", "SendTime") )
            {
                throw new ErrorException("flow.audit.copy.msg.set.fail");
            }
        }
    }
}
